/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bootstrap.jsf.bootstrap.component.output;

import com.sun.faces.application.resource.ResourceImpl;
import java.io.IOException;
import java.util.Set;
import javax.faces.application.Resource;
import javax.faces.application.ResourceHandler;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

/**
 *
 * @author alex
 */
@FacesComponent("imagePolaroid")
public class ImagePolaroid extends UIOutput {
    private String library;
    private String name;
    private String extension;
    private String id;
    private String styleClass;
    private String style;
    private String src;
    private Integer width = null;
    private Integer height = null;
    

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        setId(getClientId());
        
        if (getAttributes().containsKey("library")) {
            setLibrary(getAttributes().get("library").toString());
        }
        
        if (getAttributes().containsKey("name")) {
            setName(getAttributes().get("name").toString());
        }
        
        if (getAttributes().containsKey("extension")) {
            setExtension(getAttributes().get("extension").toString());
        }
        
        if (getAttributes().containsKey("styleClass")) {
            setStyleClass(getAttributes().get("styleClass").toString());
        }

        if (getAttributes().containsKey("style")) {
            setStyle(getAttributes().get("style").toString());
        }
        
        if (getAttributes().containsKey("width")) {
            setWidth(Integer.parseInt(getAttributes().get("width").toString()));
        }
        
        if (getAttributes().containsKey("height")) {
            setHeight(Integer.parseInt(getAttributes().get("height").toString()));
        }
        
        setSrc(context.getExternalContext().getRequestContextPath() + "/javax.faces.resource/" + getName() +"."+ getExtension() + ".jsf?ln="+getLibrary());
        
        ResponseWriter rw = context.getResponseWriter();

        rw.startElement("img", this);
        rw.writeAttribute("id", getId(), "id");
        rw.writeAttribute("class", getId() + " img-polaroid  thumbnail " + getStyleClass(), "class");
        rw.writeAttribute("style", getStyle(), "style");
        rw.writeAttribute("src", getSrc(), "src");
        if(getWidth() != null) {
            rw.writeAttribute("width", getWidth(), "width");
        }
        
        if(getHeight() != null) {
            rw.writeAttribute("height", getHeight(), "height");
        }
    }
    
     @Override
    public void encodeEnd(FacesContext context) throws IOException {
        ResponseWriter rw = context.getResponseWriter();
        rw.endElement("img");
    }
    
    /**
     * @return the library
     */
    public String getLibrary() {
        return library;
    }

    /**
     * @param library the library to set
     */
    public void setLibrary(String library) {
        this.library = library;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the extension
     */
    public String getExtension() {
        return extension;
    }

    /**
     * @param extension the extension to set
     */
    public void setExtension(String extension) {
        this.extension = extension;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the styleClass
     */
    public String getStyleClass() {
        return styleClass;
    }

    /**
     * @param styleClass the styleClass to set
     */
    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    /**
     * @return the style
     */
    public String getStyle() {
        return style;
    }

    /**
     * @param style the style to set
     */
    public void setStyle(String style) {
        this.style = style;
    }

    /**
     * @return the src
     */
    public String getSrc() {
        return src;
    }

    /**
     * @param src the src to set
     */
    public void setSrc(String src) {
        this.src = src;
    }

    /**
     * @return the width
     */
    public Integer getWidth() {
        return width;
    }

    /**
     * @param width the width to set
     */
    public void setWidth(Integer width) {
        this.width = width;
    }

    /**
     * @return the height
     */
    public Integer getHeight() {
        return height;
    }

    /**
     * @param height the height to set
     */
    public void setHeight(Integer height) {
        this.height = height;
    }
}
